package com.toket.android.pushme.screen;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ProgressBar;

import com.toket.android.pushme.R;
import com.toket.android.pushme.activity.PushMeActivity;
import com.toket.android.pushme.view.ContentView;
import com.toket.android.pushme.view.TopbarView;
import com.toket.android.utils.Shared;

public class MainScreen extends PushMeActivity{
	
	public static Context mContext;
	
	private SharedPreferences setting;
	
	private TopbarView mTopbarView;
	private ContentView mContentView;
	
	private ProgressBar progressLoader;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_screen);
		
		setting = getSharedPreferences(Shared.SHARED_NAME, MODE_PRIVATE);
		mContext = this;
		initActivity();
		
	}
	
	@Override
	protected void initLayout() {
		// TODO Auto-generated method stub
		progressLoader = (ProgressBar) findViewById(R.id.progressLoader);
		
		mTopbarView = new TopbarView(mContext);
		mContentView = new ContentView(mContext);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.action_refresh:
			mContentView.refreshLoader();
			return true;
		}
		return super.onMenuItemSelected(featureId, item);
	}

	@Override
	protected void initValue() {
		// TODO Auto-generated method stub
		mTopbarView.setEnableButton(true);
	}

	@Override
	protected void cleanUp() {
		// TODO Auto-generated method stub
		boolean isRemember = setting.getBoolean(Shared.PREF_ISREMEMBER, Shared.DEFAULT_ISREMEMBER);
		if(!isRemember){
			Editor edit = setting.edit();
			edit.putBoolean(Shared.PREF_ISLOGIN, false);
			edit.commit();
		}
	}
	
	public void logout(){
		
		Editor edit = setting.edit();
		edit.putBoolean(Shared.PREF_ISLOGIN, false);
		edit.commit();
		
		Intent goToScreen = new Intent(mContext, LoginScreen.class);
		startActivity(goToScreen);
		finish();
	}
	
	public void showProgressLoader(boolean isShowProgress){
		if(isShowProgress){
			progressLoader.setVisibility(View.VISIBLE);
		}else{
			progressLoader.setVisibility(View.GONE);
		}
	}

}
